home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / (gcc-1.37.π) / stor-layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-20  |  31.4 KB  |  949 lines  |  [TEXT/KAHL]

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Copyright (C) 1989, 1990 Apple Computer, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include <stdio.h>
  24.  
  25. #include "tree.h"
  26. #include "rtl.h"   /* For GET_MODE_SIZE */
  27.  
  28. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  29. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  30. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  31.  
  32. /* Data type for the expressions representing sizes of data types.
  33.    It is the first integer type laid out.
  34.    In C, this is int.  */
  35.  
  36. tree sizetype;
  37.  
  38. /* An integer constant with value 0 whose type is sizetype.  */
  39.  
  40. tree size_zero_node;
  41.  
  42. /* An integer constant with value 1 whose type is sizetype.  */
  43.  
  44. tree size_one_node;
  45.  
  46. #define GET_MODE_ALIGNMENT(MODE)   \
  47.   MIN (BIGGEST_ALIGNMENT,        \
  48.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  49.  
  50. /* Chain of all permanent types we have allocated since last
  51.    call to get_permanent_types.  */
  52.  
  53. tree permanent_type_chain;
  54.  
  55. /* Chain of all temporary types we have allocated in this function.  */
  56.  
  57. tree temporary_type_chain;
  58.  
  59. /* When the chains is not null, these point at the last
  60.    types on the two chains.  These help us tell whether a type
  61.    is already on a chain.  */
  62. tree permanent_type_end;
  63. tree temporary_type_end;
  64.  
  65. void init_store_layout()
  66.     {
  67.     sizetype = 0;
  68.     size_zero_node = 0;
  69.     size_one_node = 0;
  70.     permanent_type_chain = 0;
  71.     temporary_type_chain = 0;
  72.     permanent_type_end = 0;
  73.     temporary_type_end = 0;        
  74.     }
  75.  
  76. /* Put the newly-made type T
  77.    on either permanent_type_chain or temporary_type_chain.
  78.    Types that are const or volatile variants of other types
  79.    are not put on any chain, since in the gdb symbol segment
  80.    we do not make those distinctions.
  81.  
  82.    If T is already on the chain, we do nothing.  */
  83.  
  84. void
  85. chain_type (t)
  86.      tree t;
  87. {
  88.   if (TYPE_MAIN_VARIANT (t) != t)
  89.     return;
  90.   if (TREE_CHAIN (t) != 0)
  91.     return;
  92.   if (TREE_PERMANENT (t))
  93.     {
  94.       /* If T is on the chain at the end, don't chain it to itself!  */
  95.       if (t == permanent_type_end)
  96.     return;
  97.       /* Add T to the end of the chain.  */
  98.       if (permanent_type_chain == 0)
  99.     permanent_type_chain = t;
  100.       else
  101.     TREE_CHAIN (permanent_type_end) = t;
  102.       permanent_type_end = t;
  103.     }
  104.   else
  105.     {
  106.       if (t == temporary_type_end)
  107.     return;
  108.       if (temporary_type_chain == 0)
  109.     temporary_type_chain = t;
  110.       else
  111.     TREE_CHAIN (temporary_type_end) = t;
  112.       temporary_type_end = t;
  113.     }
  114. }
  115.  
  116. /* Get a chain of all permanent types made since this function
  117.    was last called.  */
  118.  
  119. tree
  120. get_permanent_types ()
  121. {
  122.   register tree tem = permanent_type_chain;
  123.   permanent_type_chain = 0;
  124.   permanent_type_end = 0;
  125.   return tem;
  126. }
  127.  
  128. /* Get a chain of all temporary types made since this function
  129.    was last called.  */
  130.  
  131. tree
  132. get_temporary_types ()
  133. {
  134.   register tree tem = temporary_type_chain;
  135.   temporary_type_chain = 0;
  136.   temporary_type_end = 0;
  137.   return tem;
  138. }
  139.  
  140. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  141.  
  142. static tree pending_sizes;
  143.  
  144. /* Nonzero means cannot safely call expand_expr now,
  145.    so put variable sizes onto `pending_sizes' instead.  */
  146.  
  147. int immediate_size_expand;
  148.  
  149. tree
  150. get_pending_sizes ()
  151. {
  152.   tree chain = pending_sizes;
  153.   pending_sizes = 0;
  154.   return chain;
  155. }
  156.  
  157. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  158.    to serve as the actual size-expression for a type or decl.  */
  159.  
  160. static tree
  161. variable_size (size)
  162.      tree size;
  163. {
  164.   size = save_expr (size);
  165.  
  166.   if (global_bindings_p ())
  167.     {
  168.       error ("variable-size type declared outside of any function");
  169.       return build_int (1);
  170.     }
  171.  
  172.   if (immediate_size_expand)
  173.     expand_expr (size, 0, VOIDmode, 0);
  174.   else
  175.     pending_sizes = tree_cons (0, size, pending_sizes);
  176.  
  177.   return size;
  178. }
  179.  
  180. /* Return the machine mode to use for an aggregate of SIZE bits.
  181.  
  182.    Note!!!  We only use a non-BLKmode mode if the size matches exactly.
  183.    There used to be the idea of using ecific
  184.    code to intialize the DECL_OFFSET of TYPE_DECL nodes.
  185.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  186.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  187.    Don't call layout_decl for them.
  188.  
  189.    KNOWN_ALIGN is the amount of alignment we can assume this
  190.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  191.    and depends on the previous fields.
  192.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  193.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  194.    the record will be aligned to suit.  */
  195.  
  196. void
  197. layout_decl (decl, known_align)
  198.      tree decl;
  199.      unsigned known_align;
  200. {
  201.   register tree type = TREE_TYPE (decl);
  202.   register enum tree_code code = TREE_CODE (decl);
  203.   int spec_size = DECL_SIZE_UNIT (decl);
  204.   int bitsize;
  205.  
  206.   if (code == CONST_DECL)
  207.     return;
  208.  
  209.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  210.       && code != FIELD_DECL && code != TYPE_DECL)
  211.     abort ();
  212.  
  213.   if (type == error_mark_node)
  214.     {
  215.       type = void_type_node;
  216.       spec_size = 0;
  217.     }
  218.   if (TYPE_SIZE_UNIT (type) == 0)
  219.     abort ();
  220.  
  221.   /* Usually the size and mode come from the data type without change.  */
  222.  
  223.   DECL_MODE (decl) = TYPE_MODE (type);
  224.   DECL_SIZE (decl) = TYPE_SIZE (type);
  225.   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
  226.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  227.  
  228.   if (code == FIELD_DECL && TREE_PACKED (decl))
  229.     {
  230.       /* This is a bit-field.  We don't know how to handle
  231.      them except for integers and enums, and front end should
  232.      never generate them otherwise.  */
  233.  
  234.       if (! (TREE_CODE (type) == INTEGER_TYPE
  235.          || TREE_CODE (type) == ENUMERAL_TYPE))
  236.     abort ();
  237.  
  238.       if (spec_size == 0)
  239.     abort ();
  240.  
  241.       /* Mode is "integer bit field".  */
  242.       DECL_MODE (decl) = BImode;
  243.       /* Size is specified number of bits.  */
  244.       DECL_SIZE (decl) = size_one_node;
  245.       DECL_SIZE_UNIT (decl) = spec_size;
  246.     }
  247.   /* Force alignment required for the data type.
  248.      But if the decl itself wants greater alignment, don't override that.  */
  249.   else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  250.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  251.  
  252.   if (DECL_SIZE (decl))
  253.     bitsize = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
  254.  
  255.   /* See if we can use a scalar mode such as QImode or SImode
  256.      in place of BLKmode or a packed byte mode.  */
  257.   /* Conditions are: a fixed size that is correct for another mode
  258.      and occupying a complete byte or bytes on proper boundary.  */
  259.   if ((DECL_MODE (decl) == BLKmode
  260.        || DECL_MODE (decl) == BImode)
  261.       /* Don't do this if DECL's type requires it to be BLKmode.  */
  262.       && TYPE_MODE (type) != BLKmode
  263.       && TYPE_SIZE (type) != 0
  264.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  265.     {
  266.       register enum machine_mode xmode = agg_mode (bitsize);
  267.  
  268.       if (xmode != BLKmode
  269.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  270.     {
  271.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  272.                    DECL_ALIGN (decl));
  273.       DECL_MODE (decl) = xmode;
  274.       DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
  275.       DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
  276.       bitsize = GET_MODE_BITSIZE (xmode);
  277.     }
  278.     }
  279.  
  280.   /* Don't let more than one word of an aggregate occupy one register,
  281.      since then the SUBREG used to access the high part would malfunction.
  282.      Check that the expected # of registers is big enough that they
  283.      seem to hold this variable with just a word per register.  */
  284.   if (DECL_SIZE (decl) != 0
  285.       && (TREE_CODE (type) == RECORD_TYPE
  286.       || TREE_CODE (type) == UNION_TYPE
  287.       || TREE_CODE (type) == ARRAY_TYPE))
  288.     {
  289.       /* This test is not exactly right, since we really want the minimum
  290.      number of regs in any class that can hold this mode.
  291.      But it does distinguish the machines we need to distinguish,
  292.      for now.  */
  293.       if (CLASS_MAX_NREGS (ALL_REGS, TYPE_MODE (type)) * BITS_PER_WORD
  294.       < bitsize)
  295.     TREE_ADDRESSABLE (decl) = 1;
  296.     }
  297.  
  298.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  299.   if (DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
  300.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  301. }
  302.  
  303. /* Lay out a RECORD_TYPE type (a C struct).
  304.    This means laying out the fields, determining their offsets,
  305.    and computing the overall size and required alignment of the record.
  306.    Note that if you set the TYPE_ALIGN before calling this
  307.    then the struct is aligned to at least that boundary.
  308.  
  309.    If the type has basetypes, you must call layout_basetypes
  310.    before calling this function.  */
  311.  
  312. static void
  313. layout_record (rec)
  314.      tree rec;
  315. {
  316.   register tree field;
  317. #ifdef STRUCTURE_SIZE_BOUNDARY
  318.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  319. #else
  320.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  321. #endif
  322.   /* These must be laid out *after* the record is.  */
  323.   tree pending_statics = NULL_TREE;
  324.   /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
  325.      where CONST_SIZE is an integer
  326.      and VAR_SIZE is a tree expression.
  327.      If VAR_SIZE is null, the size is just CONST_SIZE.
  328.      Naturally we try to avoid using VAR_SIZE.  */
  329.   register int const_size = 0;
  330.   register tree var_size = 0;
  331.   register int size_unit = BITS_PER_UNIT;
  332.  
  333. #if 0
  334.   /* If there are basetypes, the caller should already have
  335.      laid them out.  Leave space at the beginning for them.  */
  336.   if (TYPE_SIZE (rec) != 0)
  337.     {
  338.       if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  339.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  340.       else
  341.     var_size = TYPE_SIZE (rec);
  342.       size_unit = TYPE_SIZE_UNIT (rec);
  343.     }
  344. #endif /* 0 */
  345.  
  346.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  347.     {
  348.       register int desired_align;
  349.  
  350.       /* If FIELD is a VAR_DECL, then treat it like a separate variable,
  351.      not really like a structure field.
  352.      If it is a FUNCTION_DECL, it's a method.
  353.      In both cases, all we do is lay out the decl,
  354.      and we do it *after* the record is laid out.  */
  355.  
  356.       if (TREE_CODE (field) == VAR_DECL)
  357.     {
  358.       pending_statics = tree_cons (NULL, field, pending_statics);
  359.       continue;
  360.     }
  361.       /* Enumerators and enum types which are local to this class need not
  362.      be laid out.  */
  363.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  364.     continue;
  365.  
  366.       /* Lay out the field so we know what alignment it needs.
  367.      For KNOWN_ALIGN, pass the number of bits from start of record
  368.      or some divisor of it.  */
  369.  
  370.       layout_decl (field, var_size ? size_unit : const_size);
  371.       desired_align = DECL_ALIGN (field);
  372.  
  373.       /* Record must have at least as much alignment as any field.
  374.      Otherwise, the alignment of the field within the record
  375.      is meaningless.  */
  376.  
  377.       record_align = MAX (record_align, desired_align);
  378. #ifdef PCC_BITFIELD_TYPE_MATTERS
  379.       /* In PCC on Vax, Sony, etc., a bit field of declare type `int'
  380.      forces the entire structure to have `int' alignment.  */
  381.       if (DECL_NAME (field) != 0)
  382.     record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
  383. #endif
  384.  
  385.       /* Does this field automatically have alignment it needs
  386.      by virtue of the fields that precede it and the record's
  387.      own alignment?  */
  388.  
  389.       if (const_size % desired_align != 0
  390.       || (size_unit % desired_align != 0
  391.           && var_size))
  392.     {
  393.       /* No, we need to skip space before this field.
  394.          Bump the cumulative size to multiple of field alignment.  */
  395.  
  396.       if (var_size == 0
  397.           || size_unit % desired_align == 0)
  398.         const_size
  399.           = CEIL (const_size, desired_align) * desired_align;
  400.       else
  401.         {
  402.           var_size
  403.         = genop (PLUS_EXPR, var_size,
  404.              build_int (CEIL (const_size, size_unit)));
  405.           const_size = 0;
  406.           var_size = convert_units (var_size, size_unit, desired_align);
  407.           size_unit = desired_align;
  408.         }
  409.     }
  410.  
  411. #ifdef PCC_BITFIELD_TYPE_MATTERS
  412.       if (TREE_CODE (field) == FIELD_DECL
  413.       && TREE_TYPE (field) != error_mark_node)
  414.     {
  415.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  416.       register tree dsize = DECL_SIZE (field);
  417.       int field_size = TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  418.  
  419.       /* A bit field may not span the unit of alignment of its type.
  420.          Advance to next boundary if necessary.  */
  421.       if (const_size / type_align
  422.           != (const_size + field_size - 1) / type_align)
  423.         const_size = CEIL (const_size, type_align) * type_align;
  424.     }
  425. #endif
  426.  
  427.       /* Size so far becomes the offset of this field.  */
  428.  
  429.       DECL_OFFSET (field) = const_size;
  430.       DECL_VOFFSET (field) = var_size;
  431.       DECL_VOFFSET_UNIT (field) = size_unit;
  432.  
  433.       /* If this field is an anonymous union,
  434.      give each union-member the same offset as the union has.  */
  435.  
  436.       if (DECL_NAME (field) == 0
  437.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  438.     {
  439.       tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  440.       for (; uelt; uelt = TREE_CHAIN (uelt))
  441.         {
  442.           DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  443.           DECL_OFFSET (uelt) = DECL_OFFSET (field);
  444.           DECL_VOFFSET (uelt) = DECL_VOFFSET (field);
  445.           DECL_VOFFSET_UNIT (uelt) = DECL_VOFFSET_UNIT (field);
  446.         }
  447.     }
  448.  
  449.       /* Now add size of this field to the size of the record.  */
  450.  
  451.       {
  452.         register tree dsize = DECL_SIZE (field);
  453.  
  454.     if (TREE_LITERAL (dsize))
  455.       const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  456.     else if (var_size == 0)
  457.       {
  458.         var_size = dsize;
  459.         size_unit = DECL_SIZE_UNIT (field);
  460.       }
  461.     else
  462.       {
  463.         register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
  464.         var_size
  465.           = genop (PLUS_EXPR,
  466.                convert_units (var_size, size_unit, tunits),
  467.                convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
  468.       }
  469.       }
  470.     }
  471.  
  472.   /* Work out the total size and alignment of the record
  473.      as one expression and store in the record type.
  474.      Round it up to a multiple of the record's alignment.  */
  475.  
  476.   if (var_size == 0)
  477.     TYPE_SIZE (rec)
  478.       = build_int (CEIL (CEIL (const_size, record_align) * record_align,
  479.              size_unit));
  480.   else
  481.     {
  482.       if (const_size)
  483.     var_size
  484.       = genop (PLUS_EXPR, var_size,
  485.            build_int (CEIL (const_size, size_unit)));
  486.       TYPE_SIZE (rec)
  487.     = convert_units (var_size,
  488.              size_unit,
  489.              record_align);
  490.       size_unit = record_align;
  491.     }
  492.  
  493.   TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
  494.                    BITS_PER_UNIT);
  495.   TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  496.   TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
  497.  
  498.   /* Lay out any static members.  This is done now
  499.      because their type may use the record's type.  */
  500.  
  501.   for (field = pending_statics; field; field = TREE_CHAIN (field))
  502.     layout_decl (TREE_VALUE (field), 0);
  503. }
  504.  
  505. /* Lay out a UNION_TYPE type.
  506.    Lay out all the fields, set their offsets to zero,
  507.    and compute the size and alignment of the union (maximum of any field).
  508.    Note that if you set the TYPE_ALIGN before calling this
  509.    then the union align is aligned to at least that boundary.  */
  510.  
  511. static void
  512. layout_union (rec)
  513.      tree rec;
  514. {
  515.   register tree field;
  516. #ifdef STRUCTURE_SIZE_BOUNDARY
  517.   int union_align = STRUCTURE_SIZE_BOUNDARY;
  518. #else
  519.   int union_align = BITS_PER_UNIT;
  520. #endif
  521.  
  522.   /* The size of the union, based on the fields scanned so far,
  523.      is max (CONST_SIZE, VAR_SIZE).
  524.      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
  525.   register int const_size = 0;
  526.   register tree var_size = 0;
  527.  
  528.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  529.     {
  530. #if 0 /* This should be in a language-specific file
  531.      since it needs to use language-specific terminology.  */
  532.       if (TREE_STATIC (field))
  533.     {
  534.       error_with_decl (field, "field `%s' declared static in union");
  535.       TREE_STATIC (field) = 0;
  536.     }
  537. #endif
  538.  
  539.       /* Ignore enumerators and enum types local to the union.  */
  540.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  541.     continue;
  542.  
  543.       layout_decl (field, 0);
  544.       DECL_OFFSET (field) = 0;
  545.       DECL_VOFFSET (field) = 0;
  546.       DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
  547.  
  548.       /* Union must be at least as aligned as any field requires.  */
  549.  
  550.       union_align = MAX (union_align, DECL_ALIGN (field));
  551.  
  552. #ifdef PCC_BITFIELD_TYPE_MATTERS
  553.       /* On the m88000, a bit field of declare type `int'
  554.      forces the entire union to have `int' alignment.  */
  555.       union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
  556. #endif
  557.  
  558.       /* Set union_size to max (decl_size, union_size).
  559.      There are more and less general ways to do this.
  560.      Use only CONST_SIZE unless forced to use VAR_SIZE.  */
  561.  
  562.       if (TREE_LITERAL (DECL_SIZE (field)))
  563.     const_size = MAX (const_size,
  564.               TREE_INT_CST_LOW (DECL_SIZE (field))
  565.               * DECL_SIZE_UNIT (field));
  566.       else if (var_size == 0)
  567.     var_size = convert_units (DECL_SIZE (field),
  568.                   DECL_SIZE_UNIT (field),
  569.                   BITS_PER_UNIT);
  570.       else
  571.     var_size = genop (MAX_EXPR,
  572.               convert_units (DECL_SIZE (field),
  573.                      DECL_SIZE_UNIT (field),
  574.                      BITS_PER_UNIT),
  575.               var_size);
  576.     }
  577.  
  578.   /* Determine the ultimate size of the union (in bytes).  */
  579.   if (NULL == var_size)
  580.     TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
  581.   else if (const_size == 0)
  582.     TYPE_SIZE (rec) = var_size;
  583.   else
  584.     TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
  585.                  build_int (CEIL (const_size, BITS_PER_UNIT)));
  586.  
  587.   /* Determine the desired alignment.  */
  588.   union_align = MIN (BIGGEST_ALIGNMENT, union_align);
  589.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
  590.  
  591.   /* Round the size up to be a multiple of the required alignment */
  592.   TYPE_SIZE (rec)
  593.     = convert_units (TYPE_SIZE (rec), BITS_PER_UNIT, TYPE_ALIGN (rec));
  594.   TYPE_SIZE_UNIT (rec) = TYPE_ALIGN (rec);
  595. }
  596.  
  597. /* Calculate the mode, size, and alignment for TYPE.
  598.    For an array type, calculate the element separation as well.
  599.    Record TYPE on the chain of permanent or temporary types
  600.    so that dbxout will find out about it.
  601.  
  602.    TYPE_SIZE of a type is nonzero if the type has been laid out already.
  603.    layout_type does nothing on such a type.
  604.  
  605.    If the type is incomplete, its TYPE_SIZE remains zero.  */
  606.  
  607. void
  608. layout_type (type)
  609.      tree type;
  610. {
  611.   int old;
  612.   int temporary = 0;
  613.  
  614.   if (type == 0)
  615.     abort ();
  616.  
  617.   /* Do nothing if type has been laid out before.  */
  618.   if (TYPE_SIZE (type))
  619.     return;
  620.  
  621.   /* Make sure all nodes we allocate are not momentary;
  622.      they must last past the current statement.  */
  623.   old  = suspend_momentary ();
  624.   if (TREE_PERMANENT (type) && allocation_temporary_p ())
  625.     {
  626.       temporary = 1;
  627.       end_temporary_allocation ();
  628.     }
  629.  
  630.   chain_type (type);
  631.  
  632.   switch (TREE_CODE (type))
  633.     {
  634.     case LANG_TYPE:
  635.       /* This kind of type is the responsibility
  636.      of the languge-specific code.  */
  637.       abort ();
  638.  
  639.     case VOID_TYPE:
  640.       TYPE_SIZE (type) = size_zero_node;
  641.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  642.       TYPE_ALIGN (type) = 1;
  643.       TYPE_MODE (type) = VOIDmode;
  644.       break;
  645.  
  646.     case INTEGER_TYPE:
  647.     case ENUMERAL_TYPE:
  648.       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  649.     TREE_UNSIGNED (type) = 1;
  650.  
  651.       /* What follows is like agg_mode except that it ignores
  652.      MAX_FIXED_MODE_SIZE.  That applies only to structures.  */
  653.       {
  654.     enum machine_mode mode, t;
  655.  
  656.     /* Get the last mode which has this size.  */
  657.     mode = BLKmode;
  658.     for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  659.          t = (enum machine_mode) ((int) t + 1))
  660.       if (GET_MODE_BITSIZE (t) == TYPE_PRECISION (type))
  661.         mode = t;
  662.  
  663.     TYPE_MODE (type) = mode;
  664.       }
  665.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  666.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  667.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  668.       break;
  669.  
  670.     case REAL_TYPE:
  671.       {
  672.     register int prec = TYPE_PRECISION (type);
  673.     if (prec <= GET_MODE_BITSIZE (SFmode))
  674.       TYPE_MODE (type) = SFmode;
  675.     else if (prec <= GET_MODE_BITSIZE (DFmode))
  676.       TYPE_MODE (type) = DFmode;
  677. #ifdef APPLE_HAX
  678.     /* If we can live with 80/96 bits, use extended float. */
  679.     else if (prec <= GET_MODE_BITSIZE (XFmode))
  680.       TYPE_MODE (type) = XFmode;
  681. #endif /* APPLE_HAX */
  682.     else if (prec <= GET_MODE_BITSIZE (TFmode))
  683.       TYPE_MODE (type) = TFmode;
  684.     else
  685.       abort ();
  686.       }
  687.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  688.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  689.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  690.       break;
  691.  
  692.     case POINTER_TYPE:
  693.     case REFERENCE_TYPE:
  694.       TYPE_MODE (type) = Pmode;
  695.       TYPE_SIZE (type) = build_int (POINTER_SIZE / BITS_PER_UNIT);
  696.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  697.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  698.       TREE_UNSIGNED (type) = 1;
  699.       TYPE_PRECISION (type) = POINTER_SIZE;
  700.       break;
  701.  
  702.     case ARRAY_TYPE:
  703.       {
  704.     register tree index = TYPE_DOMAIN (type);
  705.     register tree length;
  706.     register tree element = TREE_TYPE (type);
  707.  
  708. /*     layout_type (element);  */
  709.     build_pointer_type (element);
  710.  
  711.     if (index == 0)
  712.       length = 0;
  713.     else
  714.       length = genop (PLUS_EXPR, size_one_node,
  715.               genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  716.                  TYPE_MIN_VALUE (index)));
  717.  
  718.     if (TREE_PACKED (type))
  719.       abort ();  /* ??? Not written yet since not needed for C.  */
  720.  
  721.     TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
  722.     if (length && TYPE_SIZE (element))
  723.       TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
  724.     TYPE_SEP (type) = TYPE_SIZE (element);
  725.     TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
  726.     TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  727.     TYPE_MODE (type) = BLKmode;
  728.     if (TYPE_SIZE (type) != 0
  729.         && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  730.         /* BLKmode elements force BLKmode aggregate;
  731.            else extract/store fields may lose.  */
  732.         && TYPE_MODE (TREE_TYPE (type)) != BLKmode
  733. #ifdef STRICT_ALIGNMENT
  734.         && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  735.         || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  736.                      * TYPE_SIZE_UNIT (type)))
  737. #endif
  738.         )
  739.       {
  740.         TYPE_MODE (type)
  741.           = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  742.               * TYPE_SIZE_UNIT (type));
  743.       }
  744.     break;
  745.       }
  746.  
  747.     case RECORD_TYPE:
  748.       layout_record (type);
  749.       TYPE_MODE (type) = BLKmode;
  750.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  751.       /* If structure's known alignment is less than
  752.          what the scalar mode would need, and it matters,
  753.          then stick with BLKmode.  */
  754. #ifdef STRICT_ALIGNMENT
  755.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  756.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  757.                        * TYPE_SIZE_UNIT (type)))
  758. #endif
  759.       )
  760.     {
  761.       tree field;
  762.       /* A record which has any BLKmode members must itself be BLKmode;
  763.          it can't go in a register.  */
  764.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  765.         {
  766.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  767.         goto record_lose;
  768.  
  769.           /* Must be BLKmode if any field crosses a word boundary,
  770.          since extract_bit_field can't handle that in registers.  */
  771.           if (DECL_OFFSET (field) / BITS_PER_WORD
  772.           != ((TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  773.                + DECL_OFFSET (field) - 1)
  774.               / BITS_PER_WORD))
  775.         goto record_lose;
  776.         }
  777.       
  778.       TYPE_MODE (type)
  779.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  780.             * TYPE_SIZE_UNIT (type));
  781.     record_lose: ;
  782.     }
  783.       break;
  784.  
  785.     case UNION_TYPE:
  786.       layout_union (type);
  787.       TYPE_MODE (type) = BLKmode;
  788.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  789.       /* If structure's known alignment is less than
  790.          what the scalar mode would need, and it matters,
  791.          then stick with BLKmode.  */
  792. #ifdef STRICT_ALIGNMENT
  793.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  794.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  795.                        * TYPE_SIZE_UNIT (type)))
  796. #endif
  797.       )
  798.     {
  799.       tree field;
  800.       /* A union which has any BLKmode members must itself be BLKmode;
  801.          it can't go in a register.  */
  802.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  803.         if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  804.           goto union_lose;
  805.  
  806.       TYPE_MODE (type)
  807.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  808.             * TYPE_SIZE_UNIT (type));
  809.     union_lose: ;
  810.     }
  811.       break;
  812.  
  813.     case FUNCTION_TYPE:
  814.     case METHOD_TYPE:
  815.       TYPE_MODE (type) = EPmode;
  816.       TYPE_SIZE (type) = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  817.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  818.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  819.       break;
  820.  
  821.     default:
  822.       abort ();
  823.     } /* end switch */
  824.  
  825.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  826.   if (TYPE_SIZE (type) != 0 && ! TREE_LITERAL (TYPE_SIZE (type)))
  827.     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
  828.  
  829.   /* Also layout any other variants of the type.  */
  830.   if (TYPE_NEXT_VARIANT (type)
  831.       || type != TYPE_MAIN_VARIANT (type))
  832.     {
  833.       tree variant;
  834.       /* Record layout info of this variant.  */
  835.       tree size = TYPE_SIZE (type);
  836.       int size_unit = TYPE_SIZE_UNIT (type);
  837.       int align = TYPE_ALIGN (type);
  838.       enum machine_mode mode = TYPE_MODE (type);
  839.  
  840.       /* Copy it into all variants.  */
  841.       for (variant = TYPE_MAIN_VARIANT (type);
  842.        variant;
  843.        variant = TYPE_NEXT_VARIANT (variant))
  844.     {
  845.       TYPE_SIZE (variant) = size;
  846.       TYPE_SIZE_UNIT (variant) = size_unit;
  847.       TYPE_ALIGN (variant) = align;
  848.       TYPE_MODE (variant) = mode;
  849.     }
  850.     }
  851.     
  852.   if (temporary)
  853.     resume_temporary_allocation ();
  854.   resume_momentary (old);
  855. }
  856.  
  857. /* Create and return a type for signed integers of PRECISION bits.  */
  858.  
  859. tree
  860. make_signed_type (precision)
  861.      int precision;
  862. {
  863.   register tree type = make_node (INTEGER_TYPE);
  864.  
  865.   TYPE_PRECISION (type) = precision;
  866.  
  867.   /* Create the extreme values based on the number of bits.  */
  868.  
  869.   TYPE_MIN_VALUE (type)
  870.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
  871.            (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
  872.               ? precision-HOST_BITS_PER_INT-1
  873.               : 0));
  874.   TYPE_MAX_VALUE (type)
  875.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
  876.            (precision-HOST_BITS_PER_INT-1 > 0
  877.             ? (1<<(precision-HOST_BITS_PER_INT-1))-1
  878.             : 0));
  879.  
  880.   /* Give this type's extreme values this type as their type.  */
  881.  
  882.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  883.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  884.  
  885.   /* The first type made with this or `make_unsigned_type'
  886.      is the type for size values.  */
  887.  
  888.   if (sizetype == 0)
  889.     sizetype = type;
  890.  
  891.   /* Lay out the type: set its alignment, size, etc.  */
  892.  
  893.   layout_type (type);
  894.  
  895.   return type;
  896. }
  897.  
  898. /* Create and return a type for unsigned integers of PRECISION bits.  */
  899.  
  900. tree
  901. make_unsigned_type (precision)
  902.      int precision;
  903. {
  904.   register tree type = make_node (INTEGER_TYPE);
  905.  
  906.   TYPE_PRECISION (type) = precision;
  907.  
  908.   /* The first type made with this or `make_unsigned_type'
  909.      is the type for size values.  */
  910.  
  911.   if (sizetype == 0)
  912.     sizetype = type;
  913.  
  914.   fixup_unsigned_type (type);
  915.   return type;
  916. }
  917.  
  918. /* Set the extreme values of TYPE based on its precision in bits,
  919.    the lay it out.  This is used both in `make_unsigned_type'
  920.    and for enumeral types.  */
  921.  
  922. void
  923. fixup_unsigned_type (type)
  924.      tree type;
  925. {
  926.   register int precision = TYPE_PRECISION (type);
  927.  
  928.   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  929.   TYPE_MAX_VALUE (type)
  930.     = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
  931.            precision-HOST_BITS_PER_INT > 0
  932.            ? ((unsigned) ~0
  933.               >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
  934.            : 0);
  935.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  936.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  937.  
  938.   /* Lay out the type: set its alignment, size, etc.  */
  939.  
  940.   layout_type (type);
  941. }
  942.  
  943.  
  944. init_layout()
  945. {
  946. #define INIT_LAYOUT
  947. #include "init.c"
  948. }
  949.